﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Utility.Json;
using Utility.Log;
using Utility.Pay.Alipay;
using Utility.Models;
using Utility;
namespace JYJJ.Web.Pages
{
    public partial class AliPay_notify : System.Web.UI.Page
    {
        NameValueCollection coll;


        protected void Page_Load(object sender, EventArgs e)
        {
            string strLog = "\r\n客户机IP:" + Request.UserHostAddress + "\r\n错误地址:" + Request.Url + "\r\n操作结果:";
            coll = Request.Form;
            SortedDictionary<string, string> sPara = GetRequestPost();

            if (sPara.Count > 0)//判断是否有带返回参数
            {
                Notify aliNotify = new Notify();
                bool verifyResult = aliNotify.Verify(sPara, coll["notify_id"], coll["sign"]);

                if (verifyResult)//验证成功
                {
                    //商户订单号
                    string out_trade_no = coll["out_trade_no"];
                    //支付宝交易号
                    string trade_no = coll["trade_no"];
                    //交易状态
                    string trade_status = coll["trade_status"];
                    AliPay_Notify notify = new AliPay_Notify();
                    notify.out_trade_no = out_trade_no;
                    notify.trade_no = trade_no;
                    notify.trade_status = trade_status;
                    notify.seller_id = coll["seller_id"]; ;
                    notify.buyer_email = coll["buyer_email"];
                    notify.gmt_create = coll["gmt_create"];
                    notify.notify_time = coll["notify_time"];
                    notify.gmt_payment = coll["gmt_payment"];
                    notify.body = coll["body"];
                    notify.total_fee = coll["total_fee"];
                    notify.price = coll["price"];
                    notify.buyer_id = coll["buyer_id"];
                    notify.notify_id = coll["notify_id"];

                    //返回执行结果
                    string strRet = "";

                    if (coll["trade_status"] == "TRADE_FINISHED")
                    {
                        LogHelper.WriteLog(strLog + coll["trade_status"]);
                    }
                    else if (coll["trade_status"] == "TRADE_SUCCESS")
                    {
                        model_Donate_Order order = BLL.ServceHelper.ServiceDonate().GetDonateOrderModelByOrderID(notify.out_trade_no);
                        if (order == null)
                        {
                            strRet = " 根据订单号(" + out_trade_no + ")查询订单为空";
                        }
                        else
                        {
                            //校验收款账号 金额 是否符合 
                            if (notify.total_fee.ToSafeDecimal() != (order.payMoney))
                            {
                                strRet = " 订单金额不符合";
                            }
                            else
                            {
                                order.Status = 1;                                
                                BLL.ServceHelper.ServiceDonate().Save(order);
                                BLL.ServceHelper.ServiceDonate().UpdateDonateMoney(order);
                                strRet = "";
                            }
                        }

                    }
                    else
                    {
                        LogHelper.WriteLog(strLog + coll["trade_status"]);
                    }
                    if (strRet == "")
                    {
                        LogHelper.WriteLog(strLog + "success");
                        Response.Write("success");  //请不要修改或删除
                    }
                    else
                    {
                        LogHelper.WriteLog(strLog + "执行不成功(" + strRet + ")");
                        Response.Write("fail");
                    }
                }
                else//验证失败
                {
                    LogHelper.WriteLog(strLog + "fail：验证失败");
                    Response.Write("fail");
                }
            }
            else
            {
                LogHelper.WriteLog(strLog + "无通知参数");
                Response.Write("无通知参数");
            }
        }

        /// <summary>
        /// 获取支付宝POST过来通知消息，并以“参数名=参数值”的形式组成数组
        /// </summary>
        /// <returns>request回来的信息组成的数组</returns>
        public SortedDictionary<string, string> GetRequestPost()
        {
            string strLog = "\r\n客户机IP:" + Request.UserHostAddress + "\r\n错误地址:" + Request.Url + "\r\n提交信息:";

            int i = 0;
            SortedDictionary<string, string> sArray = new SortedDictionary<string, string>();

            //Load Form variables into NameValueCollection variable.
            //coll = Request.Params;

            // Get names of all forms into a string array.
            String[] requestItem = coll.AllKeys;

            for (i = 0; i < requestItem.Length; i++)
            {
                sArray.Add(requestItem[i], coll[requestItem[i]]);

                strLog += "\r\n" + requestItem[i] + " : " + coll[requestItem[i]];
            }

            LogHelper.WriteLog(strLog);

            return sArray;
        }

    }
}